<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>事件冒泡</title>
    <style>
        .big{
            width: 300px;
            height: 300px;
            background-color: yellow;
            display: flex;
            justify-content: center;/*水平居中*/
            align-items: center;    /*垂直居中*/
        }
        .small{
            width: 100px;
            height: 100px;
            background-color: red;
        }
        ul{
            display: flex;
            list-style: none;
        }
        li{
            width: 100px;
            height: 80px;
            margin: 5px;
            background-color: green;
        }
        .box{
            display: flex;
            width: 500px;
            height: 700px;
            flex-wrap: wrap;
            background-color: red;
        }
        .box button{
            width: 150px;
            height: 150px;
            margin: 8px;
            background-color: hotpink;
        }
        .show{
            background-color: rosybrown;
            width: 500px;
            height: 100px;
            display: flex;
            justify-content: flex-end;
            align-items: flex-end;
        }
    </style>
</head>
<body onclick="show('body')">
    <a href="https://baidu.com">去百度</a>
    <div onclick="show('big')" class="big">
        <div onclick="show('small')" class="small">小的</div>
    </div>
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
    </ul>
    <!-- 练习按钮值显示 -->
    <div class="show"></div>
    <div class="box">
        <button>7</button>
        <button>8</button>
        <button>9</button>
        <button>4</button>
        <button>5</button>
        <button>6</button>
        <button>1</button>
        <button>2</button>
        <button>3</button>
        <button>0</button>
    </div>
</body>
<script>
    function show(txt){
        console.log(txt)    //当点击小的,父类的事件都会触发(事件冒泡)
        event.stopPropagation();    //取消事件冒泡
    }
    var ul=document.querySelectorAll("ul");
    ul[0].onclick=function(){
        var obj=event.srcElement||event.currentTarget
        console.log(obj)
    }

    var a=document.querySelectorAll("a");
    a[0].onclick=function(){
        alert("a标签");
        event.preventDefault();//取消默认行为
    }
    var box=document.querySelectorAll(".box");
    box[0].onclick=function(){
        let obj=event.srcElement||event.currentTarget;
        // console.log(obj.innerText)
        event.stopPropagation();
        let show=document.querySelectorAll(".show");
        show[0].innerText=show[0].innerText+obj.innerText;
    }
</script>
</html>